<template>
  <div>
    <h2>文档生成</h2>
    <Doc :tree="docs"></Doc>
    <!-- popup -->
    <div class="code-popup" v-if="codeToShow" @click.self="codeToShow = null">
      <pre class="code code-popup__content">{{codeToShow}}</pre>
    </div>
  </div>
</template>

<script type="text/javascript">
import classCode from '@/code/class';
import Walker from '@/walker';
import DocWalker from '@/walker/doc';

const acorn = require('acorn');

export default {
  data() {
    const comments = [];
    const classTree = acorn.parse(classCode, {
      onComment: comments,
    });
    const docs = new Walker(DocWalker, {
      comments,
      code: classCode,
    }).walk(classTree);

    return {
      docs,
      codeToShow: null,
    };
  },
  created() {
    this.$root.$on('showCode', (pos) => {
      this.codeToShow = classCode.substr(pos[0], pos[1] - pos[0]);
    });
  },
};
</script>

<style type="text/css">
.code-popup {
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.3);
}
.code-popup__content {
  position: absolute;
  width: 600px;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}
</style>
